﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InspectorWindow.cs" company="Marcelo Roca">
//   Copyright (c) 2011 Marcelo Roca. All rights reserved.
// </copyright>
// <author name="Marcelo Roca" email="marce@marceloroca.com"/>
// <license>
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see http://www.gnu.org/licenses/.
// </license>
// --------------------------------------------------------------------------------------------------------------------

namespace GameKits.Unity.SmartInspector.Editor
{
    using UnityEditor;

    using UnityEngine;

    /// <summary>
    /// Es la ventana del inspector
    /// </summary>
    public class InspectorWindow : EditorWindow
    {
        /// <summary>
        /// El gameObject que estamos tocando
        /// </summary>
        private GameObject currentGameObject;

        /// <summary>
        /// Gets LastInspector.
        /// </summary>
        public static Inspector LastInspector
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets CurrentInspector.
        /// </summary>
        private Inspector CurrentInspector
        {
            get
            {
                LastInspector = Inspector.Contains(this.currentGameObject) ?
                    Inspector.GetInspector(this.currentGameObject) : new Inspector(this.currentGameObject, this);

                return LastInspector;
            }
        }

        /// <summary>
        /// Add menu named "My Window" to the Window menu
        /// </summary>
        [MenuItem("GameKits/Smart Inspector")]
        public static void Init()
        {
            // Get existing open window or if none, make a new one:
            var window = GetWindow(typeof(InspectorWindow));

            window.title = "Inspector++";
        }

        /// <summary>
        /// Implements EditorWindows.OnGUI
        /// </summary>
        public void OnSelectionChange()
        {
            this.currentGameObject = Selection.activeGameObject;
            this.Repaint();
        }

        /// <summary>
        /// Implements EditorWindow.OnGUI
        /// </summary>
        public void OnGUI()
        {
            if (this.currentGameObject != null)
            {
                this.CurrentInspector.OnGUI();
            }
        }

        /// <summary>
        /// Implementes EditorWindow.OnHierarchyChange
        /// </summary>
        public void OnHierarchyChange()
        {
            this.CurrentInspector.Refresh();
        }
    }
}
